package hhm.android.my

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.launcher.ARouter
import hhm.android.library.update.constant.UiType
import hhm.android.library.update.listener.OnInitUiListener
import hhm.android.library.update.model.UiConfig
import hhm.android.library.update.model.UpdateConfig
import hhm.android.library.update.ui.UpdateAppActivity
import hhm.android.library.update.update.UpdateAppUtils
import hhm.android.library.utils.OnMultiClickListener
import hhm.android.my.databinding.ActivityAboutUsBinding
import siau.android.base.HttpHelper
import siau.android.base.SBApplication
import siau.android.base.SBBaseActivity
import siau.android.base.WEBVIEW_ROUTER
import siau.android.http.model.UpdateRequest
import siau.android.library.KeyString


class AboutUsActivity : SBBaseActivity() {

    private lateinit var dataBinding: ActivityAboutUsBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_about_us)
        initTitleWithLine(getString(R.string.about_us))
        dataBinding.lifecycleOwner = this

        dataBinding.activityAboutUsVersionTop.text = "V${SBApplication.version}"
        dataBinding.activityAboutUsVersionBottom.text = "V${SBApplication.version}"

        dataBinding.activityAboutUsGoToMarket.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                try {
                    val i = Intent(Intent.ACTION_VIEW)
                    i.data = Uri.parse("market://search?q=" + getString(R.string.app_name))
                    startActivity(i)
                } catch (e: Exception) {
                    Toast.makeText(this@AboutUsActivity, "您的手机没有安装Android应用市场", Toast.LENGTH_SHORT).show()
                    e.printStackTrace()
                }
            }
        })

        dataBinding.activityAboutUsCheckUpdate.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                val b = UpdateRequest(SBApplication.version)
                HttpHelper().getUpdateInfo(b)?.subscribe({
                    when (it.updateType) {
                        0 -> {//不需要更新
                            showToast("当前是最新版本")
                        }
                        1 -> {//普通更新

                            val uiConfig = UiConfig().apply {
                                uiType = UiType.CUSTOM
                                customLayoutId = R.layout.dialog_update
                                updateBtnText = getString(R.string.update_right_now)
                                cancelBtnText = getString(R.string.update_not_now)
                                downloadingToastText = getString(hhm.android.library.R.string.toast_download_apk)
                                downloadingBtnText = getString(hhm.android.library.R.string.downloading)
                                downloadFailText = getString(hhm.android.library.R.string.download_fail)
                            }

                            UpdateAppUtils.getInstance(this@AboutUsActivity).apkUrl(it.link).updateTitle(getString(R.string.find_new_version)).uiConfig(uiConfig).updateContent(it.versionContent).setOnInitUiListener(object : OnInitUiListener {
                                override fun onInitUpdateUi(activity: UpdateAppActivity, view: View?, updateConfig: UpdateConfig, uiConfig: UiConfig) {
                                    view?.findViewById<ImageView>(R.id.dialog_update_update_x)?.setOnClickListener {
                                        activity.checkIsClose()
                                    }
                                    view?.findViewById<TextView>(R.id.dialog_update_version_tv)?.text = "V${it.version}"
                                }
                            }).update()
                        }
                        else -> {

                        }
                    }
                }, {})
            }
        })

        dataBinding.activityAboutUsPhone.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + dataBinding.activityAboutUsPhone.text))
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(intent)
            }
        })

        dataBinding.activityAboutUsUserServiceAgreement.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(WEBVIEW_ROUTER).withString(KeyString.url, SBApplication.usaString).navigation()
            }
        })
        dataBinding.activityAboutUsPrivacyAgreement.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(WEBVIEW_ROUTER).withString(KeyString.url, SBApplication.privacyString).navigation()
            }
        })
    }
}